package leetCode;
import java.util.Arrays;


public class Algorithm {
	
	public static boolean checkPrime(int n){
		if(n <= 1) return false;
		if(n == 2) return true;
		if(n % 2 == 0) return false;
		int m = (int)Math.floor(Math.sqrt(n));
		
		for(int i = 3 ; i <= m ; i+=2)
			if(n % i == 0) return false;
		
		return true;
	}
	/**
	 * Sieve of Eratosthenes Algorithm
	 * Generate all primes from 2 to given integer n.
	 * @param n
	 * @return
	 */
	public static boolean[] sieve(int n){
		boolean[] result  = new boolean[n+1];
		Arrays.fill(result, true);//
		result[0] = false; result[1] = false;
		int m = (int)Math.sqrt(n);
		
		for(int i = 2 ; i <= m ; i++)
			if(result[i] == true)
				for(int j = i*i ; j <= n ; j+=i)
					result[j] = false;
		return result;
	}
	
	public static int toDecimal(String a , int b){
		return Integer.parseInt(a, b);
	}
	
	public static String fromDecimal(int n, int b){
		StringBuilder sb = new StringBuilder();
		String dic = "0123456789ABCDEFGHIJKLMN";
		while(n > 0){
			sb.insert(0, dic.charAt(n%b));
			n/=b;
		}
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stb
		//System.out.println(Algorithm.checkPrime(121));
		//System.out.println(Algorithm.sieve(121));
		//System.out.println(Algorithm.toDecimal("1400",16));
		//int a = 10; a/=2;
		//System.out.println(Integer.toHexString(234254234));
		System.out.println(Integer.parseInt(Algorithm.fromDecimal(12121, 16),16));
	}

}
